﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Utem.Server.UserContols
{
    public partial class PlayerPanel : UserControl
    {
        public readonly List<Label> playerNameList;
        public readonly List<Label> playerAnsList;
        public readonly List<Label> winList; 

        private readonly Timer timer;
        private readonly List<Color> colors;
        private int colorPos;
 
        public PlayerPanel()
        {
            InitializeComponent();

            // Initial name list.
            playerNameList = new List<Label>
            {
                lbNamePlayer1,
                lbNamePlayer2,
                lbNamePlayer3,
                lbNamePlayer4,
                lbNamePlayer5,
                lbNamePlayer6
            };

            // Initial answer list.
            playerAnsList = new List<Label>
            {
                lbAnsPlayer1,
                lbAnsPlayer2,
                lbAnsPlayer3,
                lbAnsPlayer4,
                lbAnsPlayer5,
                lbAnsPlayer6
            };

            winList = new List<Label>();

            timer = new Timer {Interval = 100, Enabled = false};
            timer.Tick += timer_Tick;

            colors = new List<Color>
            {
                Color.Red, Color.Yellow, Color.Red, Color.Yellow,
                Color.Red, Color.Yellow, Color.Red, Color.Yellow,
                Color.Red, Color.Yellow, Color.White
            };
            colorPos = 0;
        }

        void timer_Tick(object sender, EventArgs e)
        {
            for (var i = 0; i < winList.Count; i++)
            {
                winList[i].ForeColor = colors[colorPos];
            }
            
            colorPos++;
            if (colorPos > 10)
            {
                colorPos = 0;
                timer.Stop();
            }
        }

        public void SetName(string n1, string n2, string n3, string n4, string n5, string n6)
        {
            lbNamePlayer1.Text = n1;
            lbNamePlayer2.Text = n2;
            lbNamePlayer3.Text = n3;
            lbNamePlayer4.Text = n4;
            lbNamePlayer5.Text = n5;
            lbNamePlayer6.Text = n6;
        }

        public void SetName(int pos, string name)
        {
            if (pos < 0 || pos > 5) return;
            playerNameList[pos].Text = name;
        }

        public void SetAnswer(int pos, string ans)
        {
            if (pos < 0 || pos > 5) return;
            playerAnsList[pos].Text = ans;
        }

        public void SetWinner(params int[] pos)
        {
            winList.Clear();
            for (var i = 0; i < pos.Length; i++)
            {
                if (pos[i] < 0 || pos[i] > 5) continue;
                playerAnsList[pos[i]].Text = "WIN";
                winList.Add(playerAnsList[pos[i]]);
            }
            timer.Start();
        }

        public void SetLoser(params int[] pos)
        {
            winList.Clear();
            for (var i = 0; i < pos.Length; i++)
            {
                if (pos[i] < 0 || pos[i] > 5) continue;
                playerAnsList[pos[i]].Text = "LOSE";
                winList.Add(playerAnsList[pos[i]]);
            }
            timer.Start();
        }

        public void SetInCorrect(params int[] pos)
        {
            for (var i = 0; i < pos.Length; i++)
            {
                if (pos[i] < 0 || pos[i] > 5) continue;
                playerAnsList[pos[i]].ForeColor = Color.Lavender;
            }
        }

        public void Reset()
        {
            for (var i = 0; i < 6; i++)
            {
                if (playerAnsList[i].Text.Equals("WIN")) continue;
                playerAnsList[i].ForeColor = Color.Black;
                playerAnsList[i].Text = string.Empty;
            }
        }

        public void ResetAll()
        {
            for (var i = 0; i < 6; i++)
            {
                playerAnsList[i].ForeColor = Color.Black;
                playerAnsList[i].Text = string.Empty;
            }
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            base.OnSizeChanged(e);
            Size = new Size(800, 184);
        }
    }
}
